<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Using Python</title>
<meta name="GENERATOR" content="Virtual Mechanics SiteSpinner V2 270a ">
<meta http-equiv="imagetoolbar" content="false">

<style type="text/css">
<!--
.fsx01 {font-size: 11px;}
.fsx02 {font-size: 12px;}
.fsx03 {font-size: 13px;}
.fsx04 {font-size: 15px;}
.fsx05 {font-size: 16px;}
.fsx06 {font-size: 19px;}
.fsx07 {font-size: 21px;}
.fsx08 {font-size: 24px;}
.fsx09 {font-size: 27px;}
.fsx10 {font-size: 29px;}
.fsx11 {font-size: 32px;}
.fsx12 {font-size: 35px;}
.fsx13 {font-size: 37px;}
.fsx14 {font-size: 48px;}
.fsx15 {font-size: 64px;}
.fsx16 {font-size: 96px;}
.txdec {text-decoration: none;}

input,textarea,select {color:#000000;font-family:'Verdana';font-size:13px;font-weight:normal;font-style:normal;text-decoration:none;}
.dfltt {font-family:Verdana;font-size:13px;font-weight:normal;font-style:normal;text-decoration:normal;text-align:left;color:#000000;}
.dfltc {font-family:Verdana;font-size:13px;font-weight:normal;font-style:normal;text-decoration:none;text-align:left;color:#000000;}
body {margin:0;height:100%;width:100%;}
-->
</style>
</head>

<body bgcolor="#ffffff" alink="#ff0000" link="#0000ff" vlink="#800080" >

<div id="Oobj1757" style="position:absolute;z-index:1;visibility:visible; left:10px;top:10px;width:649px;height:5922px;">
<div id="Grtf1739" class="dfltt">
<font face="Courier">{%extends &quot;base.html&quot;%} {%block body%}<br>
<br>
Courier 10</font><br>
<font face="Courier New">Courier New 10</font><br>
<br>
<font face="Courier New">Questions and sections needing more work are tagged with ###<br>
<br>
CHANGE LOG:<br>
11/6/09 DMQ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; numerous clarifications, update to 3.1.<br>
4/16/09 DMQ 187+ added note on integer division.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12+ added reference to tutorial.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
3/14/09 DMQ 253+ change terminology from &quot;point&quot; to &quot;bind&quot;.</font><br>
<br>
<font face="Arial" class="fsx14" color="#0000ff"><B>Py</B></font><font face="Arial" class="fsx14" color="#ff0000"><B>Kata</B></font><font face="Arial" class="fsx08"><B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Using Python</B></font><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DRAFT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DMQ 11/21/09</font><br>
<br>
<font face="Arial" class="fsx04">This is a help page with basic topics you should read before any of the other help pages.&nbsp; The other pages may assume you understand this page.<br>
<br>
Help pages are written in tutorial style, like the more complete tutorial at <a href="http://docs.python.org/3.1/tutorial/index.html">http://docs.python.org/3.1/tutorial</a>, but focused on just what you need for the problems in each area of PyKata.</font><br>
<br>
<font face="Courier New" class="fsx06"><B>Getting Started</B></font><br>
<br>
<font face="Arial" class="fsx04">Download and install the latest version of Python 3, available from <a href="http://python.org/download">http://python.org/download</a></font><ul><font face="Arial">
<li>Note: As of November 2009, PyKata is still using the default version of Python in Google's App Engine (version 2.5).&nbsp; The differences are minor, and will not affect what the student sees in PyKata.</ul></font><font face="Arial" class="fsx04">Get familiar with the resources available at python.org, including documentation and community resources.&nbsp; Try the tutorial.<br>
<br>
Practice using IDLE, Python's Integrated Development Environment.&nbsp; IDLE is very simple, but it has all the features you will need in learning Python. Look at the IDLE Help file (from IDLE's Help menu) for a brief explanation of the menu commands and shortcut keys.&nbsp; These shortcuts are different on every system. The examples here are for Windows.<br>
<br>
Try the examples in the next section.. Practice using the shortcut keys.&nbsp; These save a lot of time when you are trying to learn a language by trial-and-error.&nbsp; Some of the most helpful are:<br>
<br>
&nbsp; <B>ALT-P, ALT-N :</B> move back and forward through the command history<br>
&nbsp; <B>Left/Right, CTRL-Left/Right, Home/End :</B> move the cursor within the command line<br>
&nbsp; <B>Backspace/Delete, CTRL Backspace/Delete :</B> delete back and forward<br>
&nbsp; <B>CTRL-K :</B> delete from cursor to end of line<br>
&nbsp; <B>CTRL-Z :</B> reverse previous edit<br>
<br>
Use the menu command File &gt; Exit to end your session.&nbsp; To stop a runaway process, <B>CTRL-C</B> usually works.&nbsp; If not, you may have to kill one or more &quot;zombie&quot; processes in memory.&nbsp; Try this now, so you won't forget what to do later.&nbsp; In Windows, use the Task Manager (<B>CTRL-ALT-DELETE</B>) and kill all processes named pythonw.exe.&nbsp; In Unix, ###<br>
<br>
Try the Python Docs command (also on the IDLE Help menu).&nbsp; This should open a new window in your web browser, showing the docs for the version of Python you are running.</font><br>
<br>
<font face="Courier New" class="fsx06"><B>Discovering Python</B></font><br>
<br>
<font face="Arial" class="fsx04">&quot;Discoverabiity&quot; was a major design goal in Python.&nbsp; This is the ability to discover how things work without a lot of effort.&nbsp; Python's <B>dir()</B> and <B>help()</B> functions are the keys to discovery.<br>
<br>
When you first start IDLE, you get something like this:</font><br>
<br>
<font face="Courier New">Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)]<br>
&gt;&gt;&gt;</font><br>
<br>
<font face="Arial" class="fsx04">The &gt;&gt;&gt; is a prompt for you to type a statement or expression to be evaluated.&nbsp; Much of the work in Python is done by calling functions.&nbsp; Here is an expression, a simple statement, and a function call with no arguments.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; 2 + 3<br>
&nbsp;&nbsp; 5<br>
&nbsp;&nbsp; &gt;&gt;&gt; n2 = 2 + 3<br>
&nbsp;&nbsp; &gt;&gt;&gt; dir()<br>
&nbsp;&nbsp; ['__builtins__', '__doc__', '__name__', '__package__', 'n2']<br>
&nbsp;&nbsp; &gt;&gt;&gt;</font><br>
<br>
<font face="Arial" class="fsx04">The expression is evaluated immediately.&nbsp; The statement evaluates an expression, and stores the result in memory.&nbsp; The <B>dir() </B>function returns a list of all the names in the directory of whatever object you provide as an argument. If you leave out the argument, the default object is the current<br>
program module, the one which the interpreter is now running (also known as '__main__').<br>
<br>
Since we have just started an interpreter session, we have only 'n2' the one variable name we have defined, plus four __special__ names added automatically at the start of every session. The double __underscores__ are a reminder not to accidentally use these names for variables in your program.&nbsp; Think of the underscores as thorns!!<br>
<br>
Names in a directory point to objects in memory.&nbsp; Let's see what each of these names points to.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; __builtins__<br>
&nbsp;&nbsp; &lt;module 'builtins' (built-in)&gt;<br>
&nbsp;&nbsp; &gt;&gt;&gt; __doc__<br>
&nbsp;&nbsp; &gt;&gt;&gt; __name__<br>
&nbsp;&nbsp; '__main__'<br>
&nbsp;&nbsp; &gt;&gt;&gt; __package__<br>
&nbsp;&nbsp; &gt;&gt;&gt; n2<br>
&nbsp;&nbsp; 5<br>
&nbsp;&nbsp; &gt;&gt;&gt;</font><br>
<br>
<font face="Arial" class="fsx04">__doc__ and __package__ have not been assigned to any object.&nbsp; __name__ has been assigned to a string object '__main__'.&nbsp; n2 has been assigned to the integer 5.&nbsp; __builtins__ is a whole separate program module.&nbsp; Let's see what is inside of it.</font><br>
<br>
<font face="Courier New">&gt;&gt;&gt; dir(__builtins__)<br>
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',<br>
'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis',<br>
'EnvironmentError', 'Exception', 'False', 'FloatingPointError',<br>
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning',<br>
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',<br>
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',<br>
'NotImplementedError', 'OSError', 'OverflowError',<br>
'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',<br>
'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning',<br>
'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError',<br>
'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',<br>
'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',<br>
'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_',<br>
'__build_class__', '__debug__', '__doc__', '__import__', '__name__',<br>
'__package__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray',<br>
'bytes', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',<br>
'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',<br>
'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',<br>
'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',<br>
'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min',<br>
'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit',<br>
'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted',<br>
'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']<br>
&gt;&gt;&gt;</font><br>
<br>
<font face="Arial" class="fsx04">Whoa!!!&nbsp; Don't panic.&nbsp; You only need a few of these to work our PyKata problems.&nbsp; There will be plenty of time later for the rest.&nbsp; Learning everything in Python can be a lifetime endeavor.&nbsp; Just remember to use the dir() function to get an overview of any new object you are working with.<br>
<br>
Let's focus now on just a few of the most useful __builtins__ (dir, help, len, list, range).&nbsp; The <B>help() </B>function provides a quick summary on the use of any object.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; help(len)<br>
&nbsp;&nbsp; Help on function len() in module __builtins__:<br>
<br>
&nbsp;&nbsp; len(...)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len(object) -&gt; integer<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return the number of items of a sequence or mapping.</font><br>
<br>
<font face="Arial" class="fsx04">How many items would you guess are in the __builtins__ directory?</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; len(dir(__builtins__))<br>
&nbsp;&nbsp; 131</font><br>
<br>
<font face="Arial" class="fsx04">dir() returns a list, and len() evaluates the length of that list.&nbsp; len() can be used also on strings and many other objects.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; len('abcde')&nbsp; # a string of 5 characters<br>
&nbsp;&nbsp; 5</font><br>
<br>
<font face="Arial" class="fsx04">A <B>list </B>is one of the most versatile objects in Python.&nbsp; It is shown as a sequence of items, separated by commas, and enclosed in square brackets.&nbsp; The items in the list above were all strings, but lists can contain any sequence of objects.&nbsp; Here we see a list with an assortment of ints, floats, strings, a function and a range object.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; list2 = [1, 2.5, 'abc', 4, 5, len, range(10)]<br>
&nbsp;&nbsp; &gt;&gt;&gt; list2<br>
&nbsp;&nbsp; [1, 2.5, 'abc', 4, 5, &lt;built-in function len&gt;, range(0, 10)]</font><br>
<br>
<font face="Arial" class="fsx04">ints, floats, and strings have a simple representation.&nbsp; Other objects may be shown as a description inside angle brackets.&nbsp; The range object is shown as a function call that will produce the object.<br>
<br>
range objects are useful whenever you need a range of integers.&nbsp; A simple list would work, but it might take up a lot of memory if the range is large.&nbsp; A range object takes the place of a list, returning one number at a time, as needed by your program.&nbsp; If you really want a complete list, all in one place, use the list function on a range object.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; list(range(-10, 10, 2))<br>
&nbsp;&nbsp; [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]</font><br>
<br>
<font face="Courier New" class="fsx06"><B>Elegant Python</B></font><br>
<br>
<font face="Arial" class="fsx04">Ranges can be used to generate more complex lists.</font><br>
<br>
<font face="Courier New">&gt;&gt;&gt; [n**2 for n in range(10)]<br>
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</font><br>
<br>
<font face="Arial" class="fsx04">Lists can be used to define functions.</font><br>
<br>
<font face="Courier New">&gt;&gt;&gt; def factors(N): return [n for n in range(2, N//2 + 1) if N % n == 0]<br>
&gt;&gt;&gt; factors(999)<br>
[3, 9, 27, 37, 111, 333]</font><br>
<br>
<font face="Arial" class="fsx04">Note the use of integer division in the second argument to the range object. N/2 would be 499.5, and range() accepts only integers as arguments.&nbsp; See <a href="http://docs.python.org/3.1/tutorial/introduction.html#numbers">http://docs.python.org/3.1/tutorial/introduction.html#numbers</a> for more on integer division.<br>
<br>
We'll have a lot more to say about lists, loops, and logic later, but for now, see if you can understand how this function works.&nbsp; Practice using the dir() and help() functions on list objects and their methods.</font><br>
<br>
<font face="Courier New">def primes(N):<br>
&nbsp;&nbsp;&nbsp; '''Return a list of the 10 largest prime numbers less than N.<br>
<br>
&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; primes(1000)<br>
&nbsp;&nbsp;&nbsp; [937, 941, 947, 953, 967, 971, 977, 983, 991, 997]<br>
&nbsp;&nbsp;&nbsp; '''<br>
&nbsp;&nbsp;&nbsp; workset = [2, 3, 5, 7]&nbsp;&nbsp; # start with all primes less than 11<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; for n in range(11, N, 2):&nbsp; # check every odd number starting with 11<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for p in workset:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if n % p == 0:&nbsp;&nbsp;&nbsp; # remainder of n//p is zero, therefore<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # n is not a prime<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; workset.append(n)&nbsp;&nbsp;&nbsp; # append n to the workset<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; return workset[-10:]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # return just the last 10 numbers</font><br>
<br>
<font face="Arial" class="fsx04">One thing you may notice about Python is that the code is all neatly and correctly indented.&nbsp; That is because the interpreter won't let you make a mistake!&nbsp; Unlike other languages, which rely on brackets and other ugly notations to define logical structure, in Python, the indentation is the structure.&nbsp; You can't be misled because the indentation is out-of-sync with the actual logic.&nbsp; Subtle errors won't go undetected until there is a disaster.<br>
<br>
Try writing a few simple functions using IDLE.&nbsp; (Just copy a problem from one of our collections.)&nbsp; See what happens if you don't indent correctly.&nbsp; At first this is a nag, but soon you will be writing perfectly formatted code without even thinking about it.&nbsp; Then when you do make a mistake, you will be happy that Python makes you fix it immediately.</font><br>
<br>
<font face="Courier New" class="fsx06"><B>Variables and Objects in Python</B></font><br>
<br>
<font face="Courier New">### Is this section appropriate for level-0, or should we move it to one of the level-1 modules? -- Dave</font><br>
<br>
<font face="Arial" class="fsx04">Python is different than other languages you may have studied.&nbsp; Variables are like labels applied to objects, not containers that hold raw data.&nbsp; Size, type, and all other attributes of the object are kept with the object itself, not the label.&nbsp; This adds a little work for the computer, but makes things simpler for the programmer.&nbsp; You never have to tell the computer the type of an object.&nbsp; It figures that out by looking at the object itself.<br>
<br>
Labels are more properly called identifiers in Python, and labeling is called &quot;binding&quot;.&nbsp; You can bind one or more identifiers to any object, and later &quot;rebind&quot; an identifier to a different object.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; a = b = c = 'a string'&nbsp; # bind three identifiers to the same object<br>
&nbsp;&nbsp; &gt;&gt;&gt; d = 'a string'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # a new object with the same value<br>
&nbsp;&nbsp; &gt;&gt;&gt; c = d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # rebind c.&nbsp; Does that rebind a and b?<br>
<br>
&nbsp;&nbsp; &gt;&gt;&gt; a == b == c == d&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # they all have the same value.<br>
&nbsp;&nbsp; True<br>
&nbsp;&nbsp; &gt;&gt;&gt; b is c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # but they are not the same object.<br>
&nbsp;&nbsp; False<br>
&nbsp;&nbsp; &gt;&gt;&gt; (a is b) and (c is d)&nbsp;&nbsp; # there are two distict objects in memory.<br>
&nbsp;&nbsp; True</font><br>
<br>
<font face="Arial" class="fsx04">Python identifiers are like sticky notes with a special kind of glue.&nbsp; They stick only to objects, not to themselves.&nbsp; For more on variables and objects in Python, see<br>
<a href="http://ece.arizona.edu/~edatools/ece175/Lecture/python-variables.htm">http://ece.arizona.edu/~edatools/ece175/Lecture/python-variables.htm</a>&nbsp;&nbsp; <br>
<a href="http://effbot.org/zone/python-objects.htm">http://effbot.org/zone/python-objects.htm</a><br>
<br>
In Python, there is no distinction between primitive values and objects. Everything is an object.&nbsp; Even a simple integer can talk.</font><br>
<br>
<font face="Courier New">&gt;&gt;&gt; Tu = 2<br>
&gt;&gt;&gt; dir(Tu)<br>
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__',<br>
'__delattr__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__',<br>
'__floordiv__', '__format__', '__ge__', '__getattribute__',<br>
'__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__',<br>
'__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__',<br>
'__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__',<br>
'__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',<br>
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',<br>
'__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',<br>
'__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__',<br>
'__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__',<br>
'conjugate', 'denominator', 'imag', 'numerator', 'real']<br>
<br>
&gt;&gt;&gt; type(Tu)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # What kind of object are you, Mr. Tu?<br>
&lt;class 'int'&gt;<br>
&gt;&gt;&gt; help(Tu)<br>
... 188 lines of info on int objects.&nbsp; See for yourself.<br>
<br>
&gt;&gt;&gt; Tu.denominator&nbsp; # If you were a fraction, what be your denominator?<br>
1<br>
&gt;&gt;&gt; Tu.imag&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # How about your imaginary part?<br>
0<br>
&gt;&gt;&gt; float(Tu)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # builtin function converts an int to a float<br>
2.0<br>
&gt;&gt;&gt; complex(Tu)&nbsp;&nbsp;&nbsp;&nbsp; # builtin function complex<br>
(2+0j)<br>
<br>
&gt;&gt;&gt; help(complex)<br>
Help on class complex in module builtins:<br>
<br>
class complex(object)<br>
&nbsp;|&nbsp; complex(real[, imag]) -&gt; complex number<br>
&nbsp;|&nbsp; <br>
&nbsp;|&nbsp; Create a complex number from a real part and an optional imaginary part.<br>
&nbsp;|&nbsp; This is equivalent to (real + imag*1j) where imag defaults to 0.<br>
&nbsp;...</font><br>
<br>
<font face="Arial" class="fsx04">On rare occasions, you may want to use primitives.&nbsp; A huge array of numbers, for example, will use less memory if we take advantage of the fact that all elements of the array are the same size and type.</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; from array import array&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # class array from module array<br>
&nbsp;&nbsp; &gt;&gt;&gt; ary2 = array('f', range(10))&nbsp; # create an array of floats<br>
&nbsp;&nbsp; &gt;&gt;&gt; ary2.append(99.5)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # array objects work like lists<br>
&nbsp;&nbsp; &gt;&gt;&gt; ary2.append('xyz')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # .. with the expected restrictions<br>
&nbsp;&nbsp;&nbsp; ...<br>
&nbsp;&nbsp; TypeError: a float is required<br>
&nbsp;&nbsp; &gt;&gt;&gt; ary2<br>
&nbsp;&nbsp; array('f', [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 99.5])<br>
&nbsp;&nbsp; &gt;&gt;&gt; </font><br>
<br>
<font face="Arial" class="fsx04">Don't try that last printout with any realistic size array, or you will have one of those situations where you need to kill the zombies!!</font><br>
<br>
<font face="Courier New" class="fsx06"><B>Libraries</B></font><br>
<br>
<font face="Arial" class="fsx04">Beyond the builtins, Python has huge collections of modules providing just about every function you can imagine.&nbsp; The most commonly-used modules are in the standard library that comes with Python.&nbsp; Others can be found in the Python Package Index at <a href="http://pypi.python.org.">http://pypi.python.org.</a>&nbsp; Let's look at one module from the standard library.</font><br>
<br>
<font face="Courier New">&gt;&gt;&gt; import math&nbsp; # load the math module from the standard library<br>
&gt;&gt;&gt; math<br>
&lt;module 'math' (built-in)&gt;<br>
<br>
&gt;&gt;&gt; dir(math)<br>
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh',<br>
'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees',<br>
'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot',<br>
'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow',<br>
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']<br>
<br>
&gt;&gt;&gt; help(math.atan2)<br>
Help on built-in function atan2 in module math:<br>
<br>
atan2(...)<br>
&nbsp;&nbsp;&nbsp; atan2(y, x)<br>
<br>
&nbsp;&nbsp;&nbsp; Return the arc tangent (measured in radians) of y/x.<br>
&nbsp;&nbsp;&nbsp; Unlike atan(y/x), the signs of both x and y are considered.</font><br>
<br>
<font face="Arial" class="fsx04">To see what other modules are available, use the command &gt;&gt;&gt; help('modules'), or look in the Library Reference at <a href="http://docs.python.org/3.1/library/">http://docs.python.org/3.1/library/</a>&nbsp; (Help menu --&gt; Python Docs).&nbsp; The docs are better organized.&nbsp; The help command is more complete, including modules you might have downloaded and installed as custom &quot;site-packages&quot;.</font><br>
<br>
<font face="Courier New">&gt;&gt;&gt; import re, cherrypy<br>
&gt;&gt;&gt; re<br>
&lt;module 're' from 'C:\Python30\lib\re.py'&gt;<br>
&gt;&gt;&gt; cherrypy<br>
&lt;module 'cherrypy' from 'C:\Python30\lib\site-packages\cherrypy\__init__.pyc'&gt;<br>
<br>
re.py</font><font face="Arial" class="fsx04"> is a single file (module) in the standard library, used for parsing &quot;regular expressions&quot;.&nbsp; </font><font face="Courier New">cherrypy</font><font face="Arial" class="fsx04"> is a complete package with 265 files in five sub-packages, a &quot;web framework&quot; useful if you ever want to write your own multi-threaded web applications.</font><br>
<br>
<font face="Courier New" class="fsx06"><B>Advanced Programming</B></font><br>
<br>
<font face="Courier New">### This section is intended as interesting anecdotal information, but I don't want it to distract from our current mission of teaching students. Perhaps it reflects too much my own bias toward teaching technical professionals. -- Dave</font><br>
<br>
<font face="Arial" class="fsx04">Python is not a toy language.&nbsp; With one big exception, it can do anything the major &quot;industrial&quot; languages can do.&nbsp; That exception is speed.&nbsp; As an interpreted language, Python cannot match the speed of a compiled, machine-oriented language like C.&nbsp; You will probably never notice a speed problem with Python, but if you do, you can write functions in C, and call them from Python. Many of Python's builtin functions are written this way.<br>
<br>
The NumPy and SciPy packages, which have vast capabilities for scientific number crunching, make good use of Python's C interface. Python and C together provide the ultimate combo of an easy-to-use interpreted language, fully integrated with a simple and fast machine-level language.<br>
<br>
To learn all you will ever need to know about programming:</font><br>
<br>
<font face="Courier New">&nbsp;&nbsp; &gt;&gt;&gt; import this<br>
&nbsp;&nbsp; ... try it :&gt;)<br>
<br>
{%endblock%}<br></font></div>
</div>

<div id="Oobj4" style="position:absolute;z-index:2;visibility:visible; left:360px;top:10px;width:216px;height:63px;">
<img name="Ggeo4" Align=Top src="../pykata.gif" alt="" border="0" width="216" height="63"></div>


</body>
</html>
